package com.gx.ring.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gx.ring.dao.RingExistCorpIDInterfaceDAO;
import com.gx.ring.entity.CorpInfo;

public class CIInterfaceImplDAO implements RingExistCorpIDInterfaceDAO<CorpInfo>{
	private Connection conn = null;
	
	public CIInterfaceImplDAO(Connection conn) {
		this.conn = conn;
	}


	@Override
	public List<CorpInfo> queryByphNumber(String phNumber) throws SQLException {
		String sql = "SELECT * FROM ring.t_corpinfo WHERE PHONENUMBER=? ";
		List<CorpInfo> ls = new ArrayList<>();
		CorpInfo corpInfo = new CorpInfo();
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setString(1, phNumber);
		ResultSet rs =  pst.executeQuery();
		while(rs.next()){
			corpInfo = saveResult(rs);
			ls.add(corpInfo);
		}
		rs.close();
		pst.close();
		return ls;
	}

	@Override
	public List<CorpInfo> queryByCorpCode(String corpCode) throws SQLException {
		String sql = "SELECT * FROM ring.t_corpinfo WHERE CORPCODE=? ";
		List<CorpInfo> ls = new ArrayList<>();
		CorpInfo corpInfo = new CorpInfo();
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setString(1, corpCode);
		ResultSet rs =  pst.executeQuery();
		while(rs.next()){
			corpInfo = saveResult(rs);
			ls.add(corpInfo);
		}
		rs.close();
		pst.close();
		return ls;
	}

	@Override
	public CorpInfo saveResult(ResultSet rs) throws SQLException {
		CorpInfo corpInfo = new CorpInfo();
		corpInfo.setAddress(rs.getString("ADDRESS"));
		corpInfo.setContactemall(rs.getString("CONTACTEMAIL"));
		corpInfo.setCorpcode(rs.getString("CORPCODE"));
		corpInfo.setCorpcontact(rs.getString("CORPCONTACT"));
		corpInfo.setCorpid(rs.getLong("CORPID"));
		corpInfo.setCorpname(rs.getString("CORPNAME"));
		corpInfo.setCorpnetkind(rs.getLong("CORPNETKIND"));
		corpInfo.setCorpplatformtype(rs.getLong("CORPPLATFORMTYPE"));
		corpInfo.setCorpspcode(rs.getString("CORPSPCODE"));
		corpInfo.setCorptype(rs.getLong("CORPTYPE"));
		corpInfo.setCreatetime(rs.getTimestamp("CREATETIME"));
		corpInfo.setDescription(rs.getString("DESCRIPTION"));
		corpInfo.setLocalid(rs.getLong("LOCALID"));
		corpInfo.setMaxdept(rs.getLong("MAXDEPT"));
		corpInfo.setMaxnoring(rs.getLong("MAXNORING"));
		corpInfo.setMaxnomember(rs.getLong("MAXNOMEMBER"));
		corpInfo.setPhonenumber(rs.getString("PHONENUMBER"));
		corpInfo.setPriority(rs.getLong("PRIORITY"));
		return corpInfo;
	}


}
